package com.bkfm.framework.core.utils;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import net.sf.json.JSONObject;

/**
 *
 * @平台：FAMSUN统一开发平台
 * @文件名称：HttpClientUtils.java @文件描述：
 * @author xq
 * @日期：2017年11月8日上午10:32:22
 *
 */
public class BkfmHttpClient {

	private static final Logger logger = Logger.getLogger(BkfmHttpClient.class);

	/**
	 *
	 * @方法没描述：json方式请求远程接口
	 * @author xq
	 * @param url
	 *            访问地址
	 * @param jsonStr
	 *            json参数字符串
	 */
	public static JSONObject httpJsonPost(String url, JSONObject jsonStr) {
		return httpJsonPost(url,jsonStr.toString());

	}

	/**
	 *
	 * @方法没描述：
	 * @author XQ
	 * @param url
	 * @param jsonStr
	 * @return
	 */
	public static JSONObject httpJsonPost(String url, String jsonStr) {
		HttpClient client = HttpClients.createDefault();
		HttpPost httppost = new HttpPost(url);
		try {
			httppost.setHeader(HTTP.CONTENT_TYPE, "application/json; charset=UTF-8");
			httppost.setHeader("Accept", "application/json; charset=UTF-8");
			if(jsonStr !=null){
				StringEntity entity = new StringEntity(jsonStr, "UTF-8");
				httppost.setEntity(entity);
			}
            HttpResponse response = client.execute(httppost);
            String resData = EntityUtils.toString(response.getEntity(), "UTF-8");
            return  JSONObject.fromObject(resData);
		} catch (Exception e) {
			logger.error("调用远程接口请求异常",e);
		} finally {
			if(httppost != null) {
				httppost.releaseConnection();
            }
		}
		return null;

	}

	/***
	 *
	 * @方法没描述：非json方式请求
	 * @author XQ
	 * @param url 请求地址
	 * @param postParam map方式的参数
	 * @return
	 */
	public static String httpPost(String url,Map<String,String> postParam){
		HttpClient client = HttpClients.createDefault();
		HttpPost httppost = new HttpPost(url);
		try {
			List<NameValuePair> params = new ArrayList<NameValuePair>();
	        //封装请求参数
	        for(String key : postParam.keySet()) {
	        	params.add(new BasicNameValuePair(key, postParam.get(key)));
	        }

			httppost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
            HttpResponse response = client.execute(httppost);
            String resData = EntityUtils.toString(response.getEntity(), "UTF-8");
            return  resData;

		} catch (Exception e) {
			logger.error("调用远程接口请求异常",e);
		} finally {
			if(httppost != null) {
				httppost.releaseConnection();
            }
		}
		return null;
	}
	
	/*public static void main(String args[]) {
	CloseableHttpClient httpclient = HttpClients.createDefault(); 
	try {
		HttpPost httppost = new HttpPost("http://localhost:8080/famsun-fpds-soap/fileupload/v1/uploadfile");

		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000)
				.build();
		httppost.setConfig(requestConfig);
		//发送附件
		FileBody bin = new FileBody(new File("D:\\3\\3.dwg"));
		StringBody comment = new StringBody("This is comment", ContentType.TEXT_PLAIN);

		HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("file", bin).addPart("comment", comment)
				.build();

		httppost.setEntity(reqEntity);

		System.out.println("executing request " + httppost.getRequestLine());
		CloseableHttpResponse response = httpclient.execute(httppost);
		try {
			System.out.println(response.getStatusLine());
			HttpEntity resEntity = response.getEntity();
			if (resEntity != null) {
				String responseEntityStr = EntityUtils.toString(response.getEntity());
				System.out.println(responseEntityStr);
				System.out.println("Response content length: " + resEntity.getContentLength());
			}
			EntityUtils.consume(resEntity);
		} finally {
			response.close();
		}
	} catch (ClientProtocolException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		try {
			httpclient.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}*/
}
